package com.rainbow.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rainbow.common.util.SecurityUtil;
import com.rainbow.modules.sys.entity.Department;
import com.rainbow.modules.sys.mapper.DepartmentMapper;
import com.rainbow.modules.sys.service.DepartmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Auth rainbow
 */
@Service
@Transactional
@Slf4j
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {

    @Autowired
    private SecurityUtil securityUtil;

    @Override
    public List<Department> findByParentIdOrderBySortOrder(String parentId, Boolean openDataFilter) {
        // 数据权限
        List<String> depIds = securityUtil.getDeparmentIds();
        if (depIds != null && depIds.size() > 0 && openDataFilter) {
            return list(new QueryWrapper<Department>().eq("parent_id", parentId).in("id", depIds));
        }
        return list(new QueryWrapper<Department>().eq("parent_id", parentId));
    }

    @Override
    public List<Department> findByParentIdAndStatusOrderBySortOrder(String parentId, Integer status) {
        return list(new QueryWrapper<Department>().eq("parent_id", parentId).eq("status", status));
    }

    @Override
    public List<Department> findByTitleLikeOrderBySortOrder(String title, Boolean openDataFilter) {
        // 数据权限
        List<String> depIds = securityUtil.getDeparmentIds();
        if (depIds != null && depIds.size() > 0 && openDataFilter) {
            return list(new QueryWrapper<Department>().like("title", title).in("id", depIds));
        }
        return list(new QueryWrapper<Department>().like("title", title));

    }
}
